package com.googlecode.bip.xml.importe;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;

/**
 * TODO: doc
 *
 * @author Claude CHOISNET
 */
// not public
class SQLQuestion extends Commiter implements Commitable
{
    private Integer idquestion;
    private SQLTopic parentSQLTopic;
    private Integer order;
    private Integer isactive;
    private Integer minvalue;
    private Integer maxvalue;
    private Integer defaultvalue;

    private List<SQLQuestionLocal> childSQLQuestionLocalList = new ArrayList<SQLQuestionLocal>();

    SQLQuestion()
    {
    }

    void setIdQuestion( int idquestion )
    {
        if( idquestion == 0 ) {
            throw new IllegalArgumentException( "idquestion=0 not allowed" );
            }

        this.idquestion = idquestion;
    }

    int getIdQuestion()
    {
        return this.idquestion;
    }

    public void setParent( SQLTopic parent )
    {
        this.parentSQLTopic = parent;
    }

    void setOrder(int order)
    {
        this.order = order;
    }

    void setIsActive(int isactive)
    {
        this.isactive = isactive;
    }

    void setIsactive( int isactive )
    {
        this.isactive = isactive;
    }

    void setMinValue( int minvalue )
    {
        this.minvalue = minvalue;
    }

    void setMaxValue( int maxvalue )
    {
        this.maxvalue = maxvalue;
    }

    void setDefaultValue( int defaultvalue )
    {
        this.defaultvalue = defaultvalue;
    }

    public void add( SQLQuestionLocal child )
    {
        if( child == null ) {
            throw new NullPointerException("child SQLQuestion is null");
            }

        childSQLQuestionLocalList.add( child );
    }

    @Override
    public void commit()
        throws NamingException, SQLException
    {
        if( order == null ) {
            throw new NullPointerException( "order is null" );
            }
        if( isactive == null ) {
            throw new NullPointerException( "isactive is null" );
            }
        if( minvalue == null ) {
            throw new NullPointerException( "minvalue is null" );
            }
        if( maxvalue == null ) {
            throw new NullPointerException( "maxvalue is null" );
            }
        if( defaultvalue == null ) {
            throw new NullPointerException( "defaultvalue is null" );
            }

        //TODO check min,max,default consitance.

        String sql = "REPLACE INTO `questions` (`idquestion`, `idtopic`, `order`, `isactive`, `minvalue`, `maxvalue`, `defaultvalue`) VALUES ('"
            + idquestion
            + "',"
            //+ (idtopic==0 ? "null" : Integer.toString( idtopic ) )
            + SQLTopic.getIdTopicSQLString( this.parentSQLTopic )
            + ",'"
            + order
            + "','"
            + isactive
            + "','"
            + minvalue
            + "','"
            + maxvalue
            + "','"
            + defaultvalue
            + "');";

        super.performeCommit( sql );

        for( SQLQuestionLocal c : childSQLQuestionLocalList ) {
            c.setParent( this );
            c.commit();
            }
    }

}
